package config

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/driver/sqlserver"
	"gorm.io/gorm"
	"log"
	"strconv"
)

var (
	MySQLDB     *gorm.DB
	SQLServerDB *gorm.DB
)

func Init() {
	initMySQLConnection(cfg)
}

func initMySQLConnection(cfg *Config) {
	username := GetMySQLConfigValue("username")
	password := GetMySQLConfigValue("password")
	host := GetMySQLConfigValue("host")
	portStr := GetMySQLConfigValue("port")
	database := GetMySQLConfigValue("database")
	port, _ := strconv.Atoi(portStr)
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?parseTime=true", username, password, host, port, database)
	var err error
	MySQLDB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
		/*开启sql打印日志*/
		//Logger: logger.New(
		//	log.New(os.Stdout, "\r\n", log.LstdFlags),
		//	logger.Config{
		//		LogLevel: logger.Info,
		//	},
		//),
	})
	if err != nil {
		log.Fatalf("无法连接到 MySQL 数据库：%v", err)
		return
	}
}
func initSQLServerConnection() {
	dsn := "sqlserver://sa:Rayo1234@192.168.10.110:14444?database=master"
	var err error
	SQLServerDB, err = gorm.Open(sqlserver.Open(dsn), &gorm.Config{})
	if err != nil {
		log.Fatalf("无法连接到 SQL Server 数据库：%v", err)
		return
	}
}
